package com.java.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.java.entity.BsCourse;
import com.java.entity.BsCourseTarget;
import com.java.mapper.BsCourseMapper;
import com.java.mapper.BsCourseTargetMapper;
import com.java.service.BsCourseService;
import com.java.util.MyMap;
import com.java.util.MyTools;
import com.java.util.resp.BaseRespone;
import com.java.util.resp.TableRespone;

@Service
public class BsCourseServiceImpl implements BsCourseService {
    private final Logger log = LoggerFactory.getLogger(getClass());

    @Autowired private BsCourseMapper courseMapper;
    @Autowired private BsCourseTargetMapper targetMapper;
    
	@Override
	public TableRespone getCourses(String title, String typeId, int page, int size) {
		MyMap mm = new MyMap()
				.put("courseTitle", title)
				.put("courseTypeId", typeId)
				.put("courseDelete", 0);
		int offset = TableRespone.getOffset(page, size);
		List<BsCourse> list = courseMapper.selectAll(mm, offset, size);
		List<BsCourse> count = courseMapper.selectAll(mm, 0, 0);
		if(count.size() > 0) {
			for (BsCourse course : list) {
				
			}
		}
		return new TableRespone(list, count.size());
	}
	
	@Override
	public BaseRespone saveCourse(BsCourse course) {
		int row = 0;
		BsCourse info = courseMapper.selectByKey(course.getCourseId());
		if(null == info) {
			course.setCourseId(MyTools.getUUID());
			course.setCourseTime(new Date());
			course.setCourseDelete(0);
			row = courseMapper.insertSelective(course);
		}else {
			row = courseMapper.updateSelective(course);
		}
		if(row > 0) {
			return BaseRespone.success("保存成功");
		}
		return BaseRespone.error("保存失败");
	}
	
	@Override
	public BaseRespone delCourse(String courseId) {
		int row = courseMapper.deleteUpdate(courseId, "course_delete");
		if(row > 0) {
			return BaseRespone.success("删除成功");
		}
		return BaseRespone.error("删除失败");
	}
	
	@Override
	public TableRespone getTargets(String courseId) {
		Map<String, Object> mm = new HashMap<String, Object>();
		mm.put("targetCourseId", courseId);
		mm.put("targetDelete", 0);
		List<BsCourseTarget> list = targetMapper.selectAllNotPage(mm);
		return new TableRespone(list, list.size());
	}

	@Override
	public BsCourseTarget getTarget(String targetId) {
		return targetMapper.selectByKey(targetId);
	}
	
	@Override
	public BaseRespone saveTarget(BsCourseTarget target) {
		int row = 0;
		BsCourseTarget info = targetMapper.selectByKey(target.getTargetId());
		if(null == info) {
			target.setTargetId(MyTools.getUUID());
			target.setTargetTime(new Date());
			target.setTargetDelete(0);
			row = targetMapper.insertSelective(target);
		}else {
			row = targetMapper.updateSelective(target);
		}
		if(row > 0) {
			return BaseRespone.success("保存成功");
		}
		return BaseRespone.error("保存失败");
	}
	
	@Override
	public BaseRespone delTarget(String targetId) {
		int row = targetMapper.deleteUpdate(targetId, "target_delete");
		if(row > 0) {
			return BaseRespone.success("删除成功");
		}
		return BaseRespone.error("删除失败");
	}
    
}
